package com.yootoo.extpmml.smg3;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.yootoo.extpmml.smg3.base.DataArea;
import com.yootoo.extpmml.smg3.base.RuleEngine;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;


/**
 * 调用规则引擎示例
 * Java 物理定义
 *
 * @author GuoZH
 * @version 1.0, 2015-08-28
 */
public class StrategiesRunner implements Serializable{

    private static final long serialVersionUID = -7926330861842640195L;
    private static final Logger logger = LoggerFactory.getLogger(StrategiesRunner.class);
    private static final String smg3Config = "smg3.properties";
    private static String alias;
    private static String signature;
    private static Integer logLevel;

    static {
        try {
            Properties properties = new Properties();
            InputStream is = StrategiesRunner.class.getClassLoader().getResourceAsStream(smg3Config);
            properties.load(is);
            IOUtils.closeQuietly(is);
            alias = properties.getProperty("da.alias");
            signature = properties.getProperty("da.signature");
            logLevel = Integer.valueOf(properties.getProperty("da.log.level","31"));
            String language = properties.getProperty("da.locale.language", "en");
            String country = properties.getProperty("da.locale.country", "US");
            Locale.setDefault( new Locale(language, country));
        } catch (IOException e) {
            logger.error("load smg3 properties err",e);
        }
    }

    /**
     * run Strategies logic
     * 参数传递给策略逻辑并执行输出结果
     *
     * @param jsonString
     */
    public static String runStrategies(String jsonString) throws IOException {
        Preconditions.checkNotNull(alias);
        Preconditions.checkNotNull(signature);
        ObjectMapper mapper = new ObjectMapper(); //转换器
        Map m = mapper.readValue(jsonString, Map.class); //json转换成map
        DataArea ctrlArea = new DataArea("OCONTROL");
        ctrlArea.setValue("ALIAS", alias);
        ctrlArea.setValue("SIGNATURE", signature);

        DataArea inArea = new DataArea("OINPUT");
        DataArea outArea = new DataArea("OOUTPUT");

        Iterator it = m.keySet().iterator();
        //将参数传递给策略
        while (it.hasNext()) {
            String key = it.next().toString();
            inArea.setValue(key, m.get(key));
        }

//        RuleEngine.loadStrategy(alias);

//        System.out.println(RuleEngine.getLoadedStrategies());

        //执行规则
        int code = RuleEngine.executeRule(logLevel, ctrlArea, inArea, outArea);
        logger.debug(code +"");
        String valueAsString = mapper.writeValueAsString(outArea.getArea());
        return valueAsString;
    }
}
